﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZX.HIS.Model;
using ZX.HIS.BLL;
using ZX.HIS.Common;
namespace ZX.HIS.UI.Controllers
{
    public class DrupInfoController : Controller
    {
        //
        // GET: /DrupInfo/
        ZX.HIS.BLL.DrupInfo bll = new BLL.DrupInfo();
        /// <summary>
        /// 显示药品信息
        /// </summary>
        /// <returns></returns>
        public ActionResult stDrupInfoIndex()
        {
            ZX.HIS.BLL.DrugTypeInfo tbll = new BLL.DrugTypeInfo();
            List<ZX.HIS.Model.DrugTypeInfo> liat = tbll.DrugTypeShow();
            ViewData["liat"] = liat;
            string name = "";
            string lei = "";
            int zhuan = 0;
            if (!String.IsNullOrEmpty(Request["namee"]))
            {
                name = Request["namee"].ToString();
                Session["namee"] = name;
            }
            if (!String.IsNullOrEmpty(Request["lei"]))
            {
                lei = Request["lei"].ToString();
                Session["lei"] = lei;
            }
            if (Request["zhuan"] != null && Request["zhuan"] != "0")
            {
                zhuan = Convert.ToInt32(Request["zhuan"].ToString());
                Session["zhuan"] = zhuan;
            }
            PageClass<ZX.HIS.Model.DrupInfo> pagelist = bll.PageShow(1, 3, name, lei, zhuan);
            return View(pagelist);
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult PageIndex(int id)
        {
            ZX.HIS.BLL.DrugTypeInfo tbll = new BLL.DrugTypeInfo();
            List<ZX.HIS.Model.DrugTypeInfo> liat = tbll.DrugTypeShow();
            ViewData["liat"] = liat;
            string name = "";
            string lei = "";
            int zhuan = 0;
            if (Session["namee"] != null)
            {
                name = Session["namee"].ToString();
            }
            if (Session["lei"] != null)
            {
                lei = Session["lei"].ToString();
            }
            if (Session["zhuan"] != null && Session["zhuan"].ToString() != "0")
            {
                zhuan = Convert.ToInt32(Session["zhuan"].ToString());
            }
            PageClass<ZX.HIS.Model.DrupInfo> pagelist = bll.PageShow(id, 3, name, lei, zhuan);
            return View("stDrupInfoIndex", pagelist);
        }
        /// <summary>
        /// 新增药品页面
        /// </summary>
        /// <returns></returns>
        public ActionResult stDrupInfoAdd()
        {
            ZX.HIS.BLL.DrugTypeInfo tbll = new BLL.DrugTypeInfo();
            ZX.HIS.BLL.OpenDose obll = new BLL.OpenDose();
            ZX.HIS.BLL.DrugUsage uebll = new BLL.DrugUsage();
            ZX.HIS.BLL.MeasuringUnit mebll = new BLL.MeasuringUnit();
            ZX.HIS.BLL.DrugFrequency Drubll = new BLL.DrugFrequency();
            ZX.HIS.BLL.Product prbll = new BLL.Product();
            List<ZX.HIS.Model.DrugTypeInfo> liat = tbll.DrugTypeShow();
            ViewData["liat"] = liat;
            List<ZX.HIS.Model.OpenDose> opmode = obll.OpenDoseShow();
            ViewData["opmode"] = opmode;
            List<ZX.HIS.Model.DrugUsage> DrugUsagemode = uebll.DrugUsageShow();
            ViewData["DrugUsagemode"] = DrugUsagemode;
            List<ZX.HIS.Model.MeasuringUnit> MeasuringUnitmode = mebll.MeasuringUnitShow();
            ViewData["MeasuringUnitmode"] = MeasuringUnitmode;
            List<ZX.HIS.Model.DrugFrequency> DrugFrequencymode = Drubll.DrugFrequencyShow();
            ViewData["DrugFrequencymode"] = DrugFrequencymode;
            List<ZX.HIS.Model.Product> Productmode = prbll.ProductList();
            ViewData["Productmode"] = Productmode;
            return View(liat);
        }
        /// <summary>
        /// 新增药品添加的方法
        /// </summary>
        /// <returns></returns>
        public int AddstDrupInfo()
        {
            ZX.HIS.Model.DrupInfo mode = new Model.DrupInfo();
            ZX.HIS.BLL.DrupInfo drbll = new BLL.DrupInfo();
            mode.OpenDoseName = Request["OpenDoseName"].ToString();
            mode.DrugName = Request["DrugName"].ToString();
            mode.DrugTypeId = Request["DrugTypeId"].ToString();
            mode.DrugIsOtc = Convert.ToInt32(Request["DrugIsOtc"].ToString());
            mode.DrugPackSpece = Request["DrugPackSpece"].ToString();
            mode.DrugState = Convert.ToInt32(Request["DrugState"].ToString());
            mode.ProductId = int.Parse(Request["ProductId"].ToString());
            mode.DrugApprovalNo = Request["DrugApprovalNo"].ToString();
            mode.DrugBarCode = Request["DrugBarCode"].ToString();
            mode.DrugPurchasePrice = Convert.ToDecimal(Request["DrugPurchasePrice"].ToString());
            mode.HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    mode.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            if (drbll.DrupInfoAdd(mode) > 0)
            {
                ZX.HIS.Model.DrugInventory drmode = new Model.DrugInventory();
                ZX.HIS.BLL.DrugInventory dbll = new BLL.DrugInventory();
                drmode.DrugInvThrVal = Convert.ToInt32(Request["DrugInvThrVal"].ToString());
                drmode.DrugInvPresPrice = Convert.ToDecimal(Request["DrugInvPresPrice"].ToString());
                drmode.SupplierId = Request["SupplierId"].ToString();
                drmode.tbDrugShelfCode = Request["tbDrugShelfCode"].ToString();
                drmode.HospitalId = 0;
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    drmode.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
                drmode.DrugId = 1001;
                if (dbll.DrugInventoryAdd(drmode) > 0)
                {
                    ZX.HIS.Model.DrugUseInfo drumode = new Model.DrugUseInfo();
                    ZX.HIS.BLL.DrugUseInfo drubll = new BLL.DrugUseInfo();
                    drumode.HospitalId = 0;
                    if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                    {
                        drumode.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                    }
                    drumode.OpenDoseName = "eee";
                    drumode.DrugUseIdMeasu = Request["DrugUseIdMeasu"].ToString();
                    drumode.DrugUseOpenDose = Request["DrugUseOpenDose"].ToString();
                    drumode.DrugUsageName = Request["DrugUsageName"].ToString();
                    drumode.MeasuringUnitName = Request["MeasuringUnitName"].ToString();
                    drumode.DrugFrequencyName = Request["DrugFrequencyName"].ToString();
                    drumode.DocAdviceId = Request["DocAdviceId"].ToString();
                    if (drubll.DrugUseInfoAdd(drumode) > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 药品修改页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult stDrupInfoUpt(int id)
        {
            ZX.HIS.BLL.DrugTypeInfo tbll = new BLL.DrugTypeInfo();
            ZX.HIS.BLL.OpenDose obll = new BLL.OpenDose();
            ZX.HIS.BLL.DrugUsage uebll = new BLL.DrugUsage();
            ZX.HIS.BLL.MeasuringUnit mebll = new BLL.MeasuringUnit();
            ZX.HIS.BLL.DrugFrequency Drubll = new BLL.DrugFrequency();
            ZX.HIS.BLL.Product prbll = new BLL.Product();
            List<ZX.HIS.Model.DrugTypeInfo> liat = tbll.DrugTypeShow();
            ViewData["liat"] = liat;
            List<ZX.HIS.Model.OpenDose> opmode = obll.OpenDoseShow();
            ViewData["opmode"] = opmode;
            List<ZX.HIS.Model.DrugUsage> DrugUsagemode = uebll.DrugUsageShow();
            ViewData["DrugUsagemode"] = DrugUsagemode;
            List<ZX.HIS.Model.MeasuringUnit> MeasuringUnitmode = mebll.MeasuringUnitShow();
            ViewData["MeasuringUnitmode"] = MeasuringUnitmode;
            List<ZX.HIS.Model.DrugFrequency> DrugFrequencymode = Drubll.DrugFrequencyShow();
            ViewData["DrugFrequencymode"] = DrugFrequencymode;
            List<ZX.HIS.Model.Product> Productmode = prbll.ProductList();
            ViewData["Productmode"] = Productmode;

            ViewBag.DrugId = id;
            return View();
        }
        /// <summary>
        /// 药品信息表修改返填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DrupInfoJson(int id)
        {
            ZX.HIS.BLL.DrupInfo drbll = new BLL.DrupInfo();
            return Json(drbll.DrupInfoUpdShow(id));
        }
        /// <summary>
        /// 药品库存的修改返填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DrugInventoryJson(int id)
        {
            ZX.HIS.BLL.DrugInventory dbll = new BLL.DrugInventory();
            return Json(dbll.DrugInventoryUpdShow(id));
        }
        /// <summary>
        /// 药品用法表的修改返填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DrugUseInfoJson(int id)
        {
            ZX.HIS.BLL.DrugUseInfo drubll = new BLL.DrugUseInfo();
            return Json(drubll.DrugUseInfoUpdShow(id));
        }
        /// <summary>
        /// 药品修改方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult UptstDrupInfo()
        {
            ZX.HIS.Model.DrupInfo mode = new Model.DrupInfo();
            ZX.HIS.BLL.DrupInfo drbll = new BLL.DrupInfo();
            mode.DrugId = Convert.ToInt32(Request["DrugId"].ToString());
            mode.OpenDoseName = Request["OpenDoseName"].ToString();
            mode.DrugName = Request["DrugName"].ToString();
            mode.DrugTypeId = Request["DrugTypeId"].ToString();
            mode.DrugIsOtc = Convert.ToInt32(Request["DrugIsOtc"].ToString());
            mode.DrugPackSpece = Request["DrugPackSpece"].ToString();
            mode.DrugState = Convert.ToInt32(Request["DrugState"].ToString());
            mode.ProductId = int.Parse(Request["ProductId"].ToString());
            mode.DrugApprovalNo = Request["DrugApprovalNo"].ToString();
            mode.DrugBarCode = Request["DrugBarCode"].ToString();
            mode.DrugPurchasePrice = Convert.ToDecimal(Request["DrugPurchasePrice"].ToString());
            mode.HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    mode.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            if (drbll.DrupInfoUpd(mode) > 0)
            {
                ZX.HIS.Model.DrugInventory drmode = new Model.DrugInventory();
                ZX.HIS.BLL.DrugInventory dbll = new BLL.DrugInventory();
                drmode.DrugId = Convert.ToInt32(Request["DrugId"].ToString());
                drmode.DrugInvThrVal = Convert.ToInt32(Request["DrugInvThrVal"].ToString());
                drmode.DrugInvPresPrice = Convert.ToDecimal(Request["DrugInvPresPrice"].ToString());
                drmode.SupplierId = Request["SupplierId"].ToString();
                drmode.tbDrugShelfCode = Request["tbDrugShelfCode"].ToString();
                drmode.HospitalId = 0;
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    mode.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
                if (dbll.DrugInventoryUpd(drmode) > 0)
                {
                    ZX.HIS.Model.DrugUseInfo drumode = new Model.DrugUseInfo();
                    ZX.HIS.BLL.DrugUseInfo drubll = new BLL.DrugUseInfo();
                    drumode.DrugUseId = Convert.ToInt32(Request["DrugId"].ToString());
                    drumode.HospitalId = 0;
                    if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                    {
                        drumode.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                    }
                    drumode.OpenDoseName = "eee";
                    drumode.DrugUseIdMeasu = Request["DrugUseIdMeasu"].ToString();
                    drumode.DrugUseOpenDose = Request["DrugUseOpenDose"].ToString();
                    drumode.DrugUsageName = Request["DrugUsageName"].ToString();
                    drumode.MeasuringUnitName = Request["MeasuringUnitName"].ToString();
                    drumode.DrugFrequencyName = Request["DrugFrequencyName"].ToString();
                    drumode.DocAdviceId = Request["DocAdviceId"].ToString();
                    if (drubll.DrugUseInfoUpd(drumode) > 0)
                    {
                        return Json(1);
                    }
                    else
                    {
                        return Json(0);
                    }
                }
                else
                {
                    return Json(0);
                }
            }
            else
            {
                return Json(0);
            }
        }

    }
}
